package com.gui.windows.components;

import com.design.DesignManager;
import com.gui.windows.UserActions;
import com.gui.*;
import com.resources.Resources;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;

/**
 * Singleton Class that manages the tools panel.
 * */
public class ToolPanel extends JToolBar implements GUIComponent,  UserActions{

	//**************************************************************************************************
	//	Constructor.
	//**************************************************************************************************		

	private static final long serialVersionUID = 1L;
	
	public ToolPanel() {
		super();
		create();
	}

	//**************************************************************************************************
	//	Attributes.
	//**************************************************************************************************

	// all available button commands on toolPanel.
	private static final String ADD = "add";
	private static final String EDIT = "edit";
	private static final String DEL = "delete";

	private static DesignManager designManager;
	
	// variable to set the tool panel enablement.
	private static boolean enabled = true;

	//**************************************************************************************************
	//	Methods that exports this Class (Public Methods).
	//**************************************************************************************************

	/**
	 * Changes the tool panel activation.
	 * @param enabled Boolean that determines the enablement of the components of the state panel. 
	 * */
	
	public void setEnabled (boolean enabled) {
		designManager = DesignManager.getDesignManager();
		ToolPanel.enabled = enabled;
		updateComponentsEnablement();
	}

	public void initFields() {}
	
	public void update(int command) {
		switch (command) {
		case CREATE_TEMPLATE:
			setEnabled(true);
			break;
		case ITEM_CREATED:
			break;
		default:
			break;
		}
	}
	
	//**************************************************************************************************
	//	Inner Methods. (Private Methods).
	//**************************************************************************************************

	public void create () {
		JButton button = null;
		button = createToolButton("add", ADD, "Adds the components selected in the seleccion panel.", "Add component");
		this.add(button);
		button = createToolButton("edit", EDIT, "Edits the components selected in the seleccion panel.", "Edit component");
		this.add(button);
		button = createToolButton("delete", DEL, "Deletes the components selected in the seleccion panel.", "Delete component");
		this.add(button);
		this.setBorder(BorderFactory.createEtchedBorder());
	}
	
	/**
	 * Updates the tool panel enablement according to the static variable {@link #enabled}.
	 * */
	private void updateComponentsEnablement () {
		for (int i = 0 ; i < this.getComponentCount() ; ++i ) {
			this.getComponent(i).setEnabled(enabled);
		}
	}

	/**
	 * Creates and configures a new button.
	 * @param imageName The name of the file used as image icon.
	 * @param actionCommand The action associated to the button to be created. 
	 * @param toolTipText The text to shown as help when passing the cursor over the button.
	 * @param altText  A brief textual description of the button image.
	 * 
	 * @return JButton
	 * */
	private JButton createToolButton (String imageName, String actionCommand, String toolTipText, String altText) {
		String imgLocation = Resources.ICON_RESOURCES + imageName + Resources.ICON_SIZE + Resources.ICON_EXTENSION;
		System.out.println(imgLocation);
		JButton button = new JButton();
		button.setActionCommand(actionCommand);
		button.setToolTipText(toolTipText);
		button.setIcon(new ImageIcon(imgLocation, altText));
		if (actionCommand.equals(ADD)) {
			button.addActionListener(new AddComponentHandler());
		} else if (actionCommand.equals(EDIT)) {
			button.addActionListener(new EditComponentHandler());
		} else if (actionCommand.equals(DEL)) {
			button.addActionListener(new DeleteComponentHandler());
		}
		button.setEnabled(false);
		return button;
	}

	//**************************************************************************************************
	//*  Buttons Handlers Classes
	//**************************************************************************************************

	private class AddComponentHandler implements ActionListener {
		public void actionPerformed (ActionEvent e) {
			designManager.createComponent(DesignManager.currentType);
		}
	}

	private class EditComponentHandler implements ActionListener {
		public void actionPerformed (ActionEvent e) {
			designManager.editComponent(DesignManager.currentItem);
		}
	}

	private class DeleteComponentHandler implements ActionListener {
		public void actionPerformed (ActionEvent e) {
			designManager.deleteComponent(DesignManager.currentItem);
		}
	}
}
